package com.yjk.mall.product.service.impl;

import com.yjk.common.to.SkuLadderTo;
import com.yjk.common.to.SpuBoundsTo;
import com.yjk.common.utils.R;
import com.yjk.mall.product.entity.*;
import com.yjk.mall.product.feign.CouponFeignService;
import com.yjk.mall.product.service.*;
import com.yjk.mall.product.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjk.common.utils.PageUtils;
import com.yjk.common.utils.Query;

import com.yjk.mall.product.dao.SpuInfoDao;
import org.springframework.transaction.annotation.Transactional;


@Service("spuInfoService")
public class SpuInfoServiceImpl extends ServiceImpl<SpuInfoDao, SpuInfoEntity> implements SpuInfoService {

    @Autowired
    SpuInfoDescService spuInfoDescService;
    @Autowired
    SkuImagesService skuImagesService;
    @Autowired
    SpuImagesService spuImagesService;
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;
    @Autowired
    CouponFeignService couponFeignService;
    @Autowired
    AttrService attrService;
    @Autowired
    ProductAttrValueService productAttrValueService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpuInfoEntity> page = this.page(
                new Query<SpuInfoEntity>().getPage(params),
                new QueryWrapper<SpuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void saveSpu(SpuSaveVo vo) {
        //保存spu_info
        SpuInfoEntity spu = new SpuInfoEntity();
        BeanUtils.copyProperties(vo, spu);
        spu.setCreateTime(new Date());
        spu.setUpdateTime(new Date());
        this.baseMapper.insert(spu);
        Long spuId = spu.getId();

        //保存product-attr
        List<BaseAttrs> baseAttrs = vo.getBaseAttrs();
        List<ProductAttrValueEntity> ProductAttrValueEntities = baseAttrs.stream().map(baseAttr -> {
            ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
            BeanUtils.copyProperties(baseAttr, productAttrValueEntity);
            productAttrValueEntity.setAttrValue(baseAttr.getAttrValues());
            productAttrValueEntity.setQuickShow(baseAttr.getShowDesc());
            productAttrValueEntity.setSpuId(spuId);
            AttrEntity attrEntity = attrService.getBaseMapper().selectById(baseAttr.getAttrId());
            productAttrValueEntity.setAttrName(attrEntity.getAttrName());
            return productAttrValueEntity;
        }).collect(Collectors.toList());
        productAttrValueService.saveBatch(ProductAttrValueEntities);

        //保存spu_info_desc
        SpuInfoDescEntity desc = new SpuInfoDescEntity();
        List<String> decript = vo.getDecript();
        desc.setDecript(String.join(",", decript));
        desc.setSpuId(spuId);
        spuInfoDescService.saveSpuDesc(desc);

        //保存spu_image
        List<String> imgs = vo.getImages();
        List<SpuImagesEntity> SpuImagesEntities = imgs.stream().map(img -> {
            SpuImagesEntity spuImagesEntity = new SpuImagesEntity();
            spuImagesEntity.setImgUrl(img);
            if (!"".equals(spuImagesEntity.getImgUrl())) {
                spuImagesEntity.setSpuId(spuId);
            }
            return spuImagesEntity;
        }).collect(Collectors.toList());
        spuImagesService.saveBatch(SpuImagesEntities);

        //保存sku-info和sku-image
        vo.getSkus().stream().forEach(sku -> {
            SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
            BeanUtils.copyProperties(sku, skuInfoEntity);
            List<SkuImagesEntity> skuimgs = new ArrayList<>();
            for (Images img : sku.getImages()) {
                if (!"".equals(img.getImgUrl())) {
                    SkuImagesEntity skuImagesEntity = new SkuImagesEntity();
                    skuImagesEntity.setImgUrl(img.getImgUrl());
                    if (img.getDefaultImg() == 1) {
                        skuInfoEntity.setSkuDefaultImg(img.getImgUrl());
                        skuImagesEntity.setDefaultImg(1);
                    }
                    skuimgs.add(skuImagesEntity);
                }
            }
            skuInfoEntity.setSpuId(spuId);
            skuInfoEntity.setCatalogId(vo.getCatalogId());
            skuInfoEntity.setBrandId(vo.getBrandId());
            skuInfoEntity.setSkuDesc(String.join(",", sku.getDescar()));
            skuInfoService.save(skuInfoEntity);
            Long skuId = skuInfoEntity.getSkuId();
            List<SkuImagesEntity> SkuImagesEntities = skuimgs.stream().map(item -> {
                item.setSkuId(skuId);
                return item;
            }).collect(Collectors.toList());
            skuImagesService.saveBatch(SkuImagesEntities);

            //保存attr
            List<Attr> attrs = sku.getAttr();
            List<SkuSaleAttrValueEntity> skuSaleAttrValueEntities = attrs.stream().map(attr -> {
                SkuSaleAttrValueEntity skuSaleAttrValueEntity = new SkuSaleAttrValueEntity();
                BeanUtils.copyProperties(attr, skuSaleAttrValueEntity);
                skuSaleAttrValueEntity.setSkuId(skuId);
                return skuSaleAttrValueEntity;
            }).collect(Collectors.toList());
            skuSaleAttrValueService.saveBatch(skuSaleAttrValueEntities);

            //保存sku-ladder
            if (!(sku.getDiscount().compareTo(BigDecimal.valueOf(0)) ==0) &&sku.getFullCount()!=0) {
                SkuLadderTo to = new SkuLadderTo();
                BeanUtils.copyProperties(sku, to);
                to.setSkuId(skuId);
                couponFeignService.saveSkuLadder(to);
            }

        });

        //保存sku-ladder
        SpuBoundsTo bto = new SpuBoundsTo();
        BeanUtils.copyProperties(vo.getBounds(), bto);
        bto.setSpuId(spuId);
        couponFeignService.saveSpuBounds(bto);
    }
}